<template>
  <div class="channel-edit">
    <van-cell title="我的频道" :border="false">
      <van-button
        size="mini"
        round
        type="danger"
        plain
        @click="isEdit = !isEdit"
      >{{ isEdit ? '完成': '编辑' }}</van-button>
    </van-cell>
    <van-grid :gutter="4">
      <van-grid-item
        class="channel-item"
        v-for="(channelItem, index) in userChannels"
        :key="channelItem.id"
        @click="onMyChannelClick(channelItem, index)">
        <span class="text" :class="{active: active === index}" slot="text">{{ channelItem.name }}</span>
        <!-- 通过插槽自定义图标 -->
          <van-icon v-show="isEdit && index!==0" slot="icon" name="close" />
        </van-grid-item>
    </van-grid>
    <van-cell title="频道推荐" :border="false"></van-cell>
    <van-grid class="recommend-grid" :gutter="4">
        <van-grid-item
          class="channel-item"
          v-for="channel in recommendChannels"
          :key="channel.id"
          :text="channel.name"
          @click="addChannel(channel)"
          icon="plus" />
      </van-grid>
  </div>
</template>

<script>
import { getAllChannels } from '@/api/article.js'
export default {
  name: 'ChannelEdit',
  components: {},
  props: {
    myChannels: {
      type: Array,
      required: true
    },
    active: {
      type: Number,
      required: true
    }
  },
  data () {
    return {
      allChannels: [], // 所有频道
      isEdit: false, // 控制编辑状态
      userChannels: this.myChannels
    }
  },
  computed: {
    recommendChannels () {
      return this.allChannels.filter(channel => {
        const mychannel = this.myChannels.find(myChannel => {
          return myChannel.id === channel.id
        })
        return !mychannel
      })
    }
  },
  watch: {
    mychannels (newVal, oldVal) {
      console.log(newVal)
      console.log(oldVal)
    }
  },
  created () {
    this.loadAllChannels()
  },
  mounted () {},
  methods: {
    // 加载所有频道
    async loadAllChannels () {
      try {
        const { data } = await getAllChannels()
        this.allChannels = data.data.channels
      } catch (err) {
        this.$toast('获取频道列表数据失败')
      }
    },
    addChannel (channel) {
      this.userChannels.push(channel)
    },
    // 点击我的频道
    onMyChannelClick (channelItem, index) {
      if (this.isEdit) {
        // 执行删除操作

        if (index === 0) return

        if (index <= this.active) {
          this.$emit('update-active', this.active - 1, true)
        }
        this.userChannels.splice(index, 1)
      } else {
        // 执行跳转操作
        this.$emit('update-active', index)
      }
    }
  }
}
</script>

<style scoped lang="less">
.channel-edit {
  .channel-item {
    height: 86px;
    /deep/ .van-grid-item__content {
      background-color: #f5f5f6;
      .van-grid-item__text {
        color: #222;
        font-size: 28px;
      }
    }
  }
}
/deep/ .recommend-grid {
    .van-grid-item__content {
        flex-direction: row;
        color: #222;
        .van-icon {
            font-size: 24px;
        }

        .van-grid-item__text {
            font-size: 28px;
            margin-top: 0;
        }
    }
}
/deep/ .van-icon-close {
    position: absolute;
    right: -10px;
    top: -10px;
    font-size: 36px;
    color: #ccc;
}
.van-icon-close:before {
    content: '\e690';
    position: absolute;
    z-index: 1;
    top: -22px;
    right: -90px;}
.van-grid-item__icon-wrapper {
        position: unset;
      }
/deep/ .van-grid-item__content {
      background-color: #f5f5f6;
      .van-grid-item__text, .text {
        color: #222;
        font-size: 28px;
        white-space: nowrap;
      }

      .active {
        color: red;
      }
    }
</style>
